from flask import Blueprint, jsonify, make_response, request

from service.LoginService import verify_login
from util.LoginUtil import set_login_info, get_login_info, set_login_out
from util.OAuthQQ import OAuthQQ

api_login = Blueprint('api_login', __name__)
api_login.url_prefix = '/api/login'


@api_login.route('/in', methods=['POST'])
def set_login():
    user = request.json
    if verify_login(user):
        response = make_response({'code': 200, 'msg': 'login success', "data": user["is_admin"]})
        set_login_info(response, user)
        return response
    else:
        return {'code': 201, 'msg': 'failed', 'data': []}


@api_login.route('/info')
def get_login():
    login_info = get_login_info()
    return jsonify({"code": 200, "msg": "", "data": login_info})


@api_login.route('/out')
def login_out():
    response = make_response({'code': 200, 'msg': 'login out'})
    set_login_out(response)
    return response


@api_login.route("/qq", methods=['POST'])
def login_qq():
    authQQ = OAuthQQ()
    url = authQQ.generate_qq_login_url('common/login-success.html')
    return {"code": 200, "msg": "not ready", "data": url}
